<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="950px"
        :clickModalClose="true"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="138px" :rules="formRules" :inline="true">
        <el-form-item label="合同编号" prop="contractId">
          <el-input class="w-[285px]" v-model="formData.contractId" placeholder="请输入合同编号"/>
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input class="w-[285px]" v-model="formData.contractName" placeholder="请输入合同名称"/>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input class="w-[285px]" v-model="formData.customerName" placeholder="请输入客户名称"/>
        </el-form-item>
        <el-form-item label="合同金额" prop="amount">
          <el-input class="w-[285px]" v-model="formData.amount" placeholder="请输入合同金额"/>
        </el-form-item>
        <el-form-item label="合同类型" prop="amount">
          <el-input class="w-[285px]" v-model="formData.amount" placeholder="请输入合同类型"/>
        </el-form-item>
        <el-form-item label="客户签约人" prop="amount">
          <el-input class="w-[285px]" v-model="formData.amount" placeholder="请输入客户签约人"/>
        </el-form-item>
        <el-form-item label="我方签约人" prop="amount">
          <el-input class="w-[285px]" v-model="formData.amount" placeholder="请输入我方签约人"/>
        </el-form-item>
        <el-form-item label="跟进状态" prop="amount">
          <el-input class="w-[285px]" v-model="formData.amount" placeholder="请输入跟进状态"/>
        </el-form-item>
        <el-form-item label="合同开始时间" prop="contractStartTime">
          <el-date-picker
              class="flex-1 !flex w-[285px]"
              v-model="formData.contractStartTime"
              type="datetime"
              clearable
              value-format="YYYY-MM-DD hh:mm:ss"
              placeholder="请选择合同开始时间"
          />
        </el-form-item>
        <el-form-item label="合同结束时间" prop="contractEndTime">
          <el-date-picker
              class="flex-1 !flex w-[285px]"
              v-model="formData.contractEndTime"
              type="datetime"
              clearable
              value-format="YYYY-MM-DD hh:mm:ss"
              placeholder="请选择合同结束时间"
          />
        </el-form-item>
        <el-form-item label="下次跟进时间" prop="contractStartTime">
          <el-date-picker
              class="flex-1 !flex w-[285px]"
              v-model="formData.contractStartTime"
              type="datetime"
              clearable
              value-format="YYYY-MM-DD hh:mm:ss"
              placeholder="请选择下次跟进时间"
          />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type {FormInstance} from 'element-plus'
import {contractEdit, contractAdd, contractDetail} from '@/api/contract'
import type Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type {PropType} from 'vue'

defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑新建合同基本信息' : '新增新建合同基本信息'
})

const formData = reactive({
  id: ''
})

const formRules = {
  id: [
    {
      required: true,
      message: '请输入主键ID',
      trigger: ['blur']
    }
  ]
}

const handleSubmit = async () => {
  await formRef.value?.validate()
  const data: any = {...formData}
  mode.value == 'edit' ? await contractEdit(data) : await contractAdd(data)
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  const data = await contractDetail({
    id: row.id
  })
  setFormData(data)
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
